package zhongbo.java.air.dictionary.web.controller;

import cn.hutool.core.date.DateTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zhongbo.java.air.dictionary.exception.BusinessException;
import zhongbo.java.air.dictionary.exception.ExceptionCode;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.model.AirPort;
import zhongbo.java.air.dictionary.model.enums.AirPortStatus;
import zhongbo.java.air.dictionary.model.enums.AirStatus;
import zhongbo.java.air.dictionary.service.IAirPortService;
import zhongbo.java.air.dictionary.vo.PageVo;
import zhongbo.java.air.dictionary.vo.RestResultVo;

@RestController
@RequestMapping("/airport")
@Api(tags = "机场基础信息操作的相关接口")
public class AirPortController {
    @Autowired
    IAirPortService airPortService;

    @PostMapping("add")
    @ApiOperation("新增机场")
    RestResultVo<AirPort> add(AirPort airPort){
            airPort.setCreateTime(DateTime.now());
            airPort.setUpdateTime(DateTime.now());
            airPort.setStatus(AirPortStatus.CLOSED);
            RestResultVo<AirPort> restResultVo = CheckData(airPort);
            AirPort data = restResultVo.getData();
            if (data == null || data.equals("")){
                 String massage = restResultVo.getMassage();
                 return RestResultVo.failed(massage);
             }
            int addAir = airPortService.addAirPort(airPort);
            if (addAir <= 0){
               return RestResultVo.failed("添加失败");
            }
            return RestResultVo.ok(airPort);
    }

    private RestResultVo<AirPort> CheckData(AirPort airPort) {
        if (airPort== null && airPort.equals("")){
            return RestResultVo.failed("信息为空");
        }
        String Icao = airPort.getIcaoCode();
        String iataCode = airPort.getIataCode();
        String timezone = airPort.getTimezone();
        if (!Icao.matches("^[A-Z]{1,4}$")){
            return RestResultVo.failed("ICAO机场代码不规范(例:ZBAA)");
        }
        if (!iataCode.matches("^[A-Z]{1,3}$")){
            return RestResultVo.failed("IATA代码不规范(例:PEK)");
        }
        if (!timezone.matches("^[A-Z]{1}[a-z]{2,6}[/][A-Z]{1}[a-z]{1,9}$")){
            return RestResultVo.failed("时区格式不规范(例:Asia/Shanghai)");
        }
        return RestResultVo.ok(airPort);
    }

    @GetMapping("delete")
    @ApiOperation("删除信息")
    RestResultVo<AirPort> delete(String id){
        if (id == null && id.equals("")) {
           return RestResultVo.failed("id为空");
        }
        AirPort airPort = airPortService.selectById(id);
        airPortService.deleteAirPort(id);
        return RestResultVo.success("删除成功! 原数据：" + airPort);
    }

    @PostMapping("update")
    @ApiOperation("更新信息")
    RestResultVo<AirPort> update(String id){
        AirPort airPort= airPortService.selectById(id);
        if (airPort == null && airPort.equals("")){
            RestResultVo.failed("数据信息为空");
        }
        airPort.setUpdateTime(DateTime.now());
        airPort.setStatus(AirPortStatus.OPERATIONAL);
        airPort.setTimezone("Asia/ChengDu");
        int update = airPortService.updateAirPort(airPort);
        if (update <= 0){
            return RestResultVo.failed("更新失败");
        }
        return RestResultVo.success("更新成功");
    }

    @GetMapping("datil")
    @ApiOperation("飞机详细信息")
    RestResultVo<AirPort> selectById(String id){
        if (id == null && id.equals("")){
            throw new BusinessException(ExceptionCode.ID_NULL);
        }
        AirPort airPort = airPortService.selectById(id);
        if (airPort == null && airPort.equals("")){
            return RestResultVo.failed("查询失败！");
        }
        return RestResultVo.ok(airPort);
    }

    @GetMapping("page")
    @ApiOperation("分页查询")
    public RestResultVo<PageVo<AirPort>> page(String name, Integer pageNumber, Integer pageSize){
        PageVo<AirPort> pageVo = airPortService.page(name,pageNumber,pageSize);
        return RestResultVo.ok(pageVo);
    }
}
